package com.zf.generator.templates;

import com.zf.generator.bean.*;

import java.util.List;

public class JspListTemplate {
	protected static String nl;

	public static synchronized JspListTemplate create(String lineSeparator) {
		nl = lineSeparator;
		JspListTemplate result = new JspListTemplate();
		nl = null;
		return result;
	}

	public final String NL = nl == null ? (System.getProperties()
			.getProperty("line.separator")) : nl;
	protected final String TEXT_1 = "<%@ page contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>"
			+ NL
			+ "<%@ include file=\"/WEB-INF/jsp/commons/taglibs.jsp\"%>"
			+ NL
			+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
			+ NL + "<html>" + NL + "<head>" + NL + "<title>";
	protected final String TEXT_2 = "列表</title>" + NL
			+ "<%@ include file=\"/WEB-INF/jsp/commons/meta.jsp\"%>" + NL
			+ "</head>" + NL + "<body>" + NL
			+ "<div id=\"container\" class=\"container_24\">" + NL
			+ "  <!-- header -->" + NL
			+ "  <%@ include file=\"/WEB-INF/jsp/commons/header.jsp\"%>" + NL
			+ "  " + NL + "  <!-- content -->" + NL
			+ "  <div id=\"content\" class=\"container_24\">" + NL
			+ "    <div class=\"container_24\">" + NL
			+ "      <div class=\"area2\">" + NL + "        <!-- 分页显示演示开始 -->"
			+ NL + "        <form id=\"listForm\" action=\"${ctx}/";
	protected final String TEXT_3 = "/";
	protected final String TEXT_4 = "/list\" method=\"get\">"
			+ NL
			+ "          <input type=\"hidden\" name=\"pageNo\" id=\"pageNo\" value=\"${pageData.pagination.pageNo}\" />"
			+ NL
			+ "          <input type=\"hidden\" name=\"fieldName\" id=\"fieldName\" value=\"${pageData.compositor.fieldName}\" />"
			+ NL
			+ "          <input type=\"hidden\" name=\"compositorType\" id=\"compositorType\" value=\"${pageData.compositor.compositorType}\" />"
			+ NL
			+ "          <table>"
			+ NL
			+ "            <tr align=\"center\">"
			+ NL
			+ "              <th>id: <y:search fieldType=\"I\" fieldList=\"id\" matchType=\"EQ\" />"
			+ NL
			+ "                <input type=\"button\" value=\"搜索\" class=\"submit\" onclick=\"search()\"/>"
			+ NL + "                <a href=\"${ctx}/";
	protected final String TEXT_5 = "/";
	protected final String TEXT_6 = "/new\">添加新";
	protected final String TEXT_7 = "</a>" + NL + "              </th>" + NL
			+ "            </tr>" + NL + "          </table>" + NL
			+ "        </form>" + NL + "      </div>" + NL
			+ "      <div class=\"area\">   " + NL
			+ "          <table  class=\"listtable\">" + NL
			+ "            <tr class=\"line\">";
	protected final String TEXT_8_ID = NL
			+ "\t\t\t\t<th><a href=\"javascript:sort('id')\">id</a></th>" + NL
			+ "\t\t\t\t";
	protected final String TEXT_8 = NL
			+ "\t\t\t\t<th><a href=\"javascript:sort('";
	protected final String TEXT_9 = "')\">";
	protected final String TEXT_10 = "</a></th>" + NL + "\t\t\t";
	protected final String TEXT_11 = NL
			+ "\t\t\t\t<th>插入时间</th>"
			+ NL
			+ "              \t<th>修改时间</th>"
			+ NL
			+ "              \t<th>操作</th>"
			+ NL
			+ "            </tr>"
			+ NL
			+ "            <c:forEach var=\"entity\" items=\"${pageData.result}\">"
			+ NL
			+ "              <tr <c:if test=\"${!entity.visible}\">bgcolor=\"#AFEEEE\"</c:if>>"
			+ NL + "              \t";
	protected final String TEXT_12_ID = NL + "\t\t\t\t\t<td>${entity.id}</td>"
			+ NL + "\t\t\t\t";
	protected final String TEXT_12 = NL + "\t\t\t\t\t<td>${entity.";
	protected final String TEXT_13 = "}</td>" + NL + "\t\t\t\t";
	protected final String TEXT_14 = NL
			+ "\t\t\t\t\t<td>${entity.insertTime}</td>" + NL
			+ "                \t<td>${entity.lastUpdateTime}</td>" + NL
			+ "                <td>" + NL
			+ "                \t<a href=\"${ctx}/";
	protected final String TEXT_15 = "/";
	protected final String TEXT_16 = "/edit/${entity.id}\">修改</a> |" + NL
			+ "                \t<a href=\"${ctx}/";
	protected final String TEXT_17 = "/";
	protected final String TEXT_18 = "/delete/${entity.id}\" onClick=\"return confirm('删除后无法恢复,确定要删除吗?');\">刪除</a> |"
			+ NL
			+ "                  <c:choose>"
			+ NL
			+ "                    <c:when test=\"${entity.visible}\"> "
			+ NL
			+ "                    \t显示 |  <a href=\"${ctx}/";
	protected final String TEXT_19 = "/";
	protected final String TEXT_20 = "/unVisible/${entity.id}\">不显示</a>" + NL
			+ "                    </c:when>" + NL
			+ "                    <c:otherwise> " + NL
			+ "                    \t<a href=\"${ctx}/";
	protected final String TEXT_21 = "/";
	protected final String TEXT_22 = "/visible/${entity.id}\">显示</a> | 不显示 "
			+ NL
			+ "                    </c:otherwise>"
			+ NL
			+ "                  </c:choose>"
			+ NL
			+ "                </td>"
			+ NL
			+ "              </tr>"
			+ NL
			+ "            </c:forEach>"
			+ NL
			+ "          </table>"
			+ NL
			+ "                           第${pageData.pagination.pageNo}页,共${pageData.pagination.totalPage}页 <a href=\"javascript:jumpPage(1)\">首页</a>"
			+ NL
			+ "          <c:if test=\"${pageData.pagination.hasPrevPage}\"> <a href=\"javascript:jumpPage(${pageData.pagination.prevPage})\">上一页</a> </c:if>"
			+ NL
			+ "          <c:if test=\"${pageData.pagination.hasNextPage}\"> <a href=\"javascript:jumpPage(${pageData.pagination.nextPage})\">下一页</a> </c:if>"
			+ NL
			+ "          <a href=\"javascript:jumpPage(${pageData.pagination.totalPage})\">末页</a>"
			+ NL + "        " + NL + "        <!-- 分页显示演示结束 -->" + NL
			+ "      </div>" + NL + "    </div>" + NL + "  </div>" + NL + "  "
			+ NL + "  <!-- footer -->" + NL
			+ "  <%@ include file=\"/WEB-INF/jsp/commons/footer.jsp\"%>" + NL
			+ "  " + NL + "</div>" + NL + "</body>" + NL + "</html>";
	protected final String TEXT_23 = NL;

	public String generate(Object argument) {
		final StringBuffer stringBuffer = new StringBuffer();

		JspTemplateArgs args = (JspTemplateArgs) argument;

		String modeName = args.getModeName();
		String entityName = args.getEntityName();
		String jspName = args.getJspName();

		List<EntityFieldInfo> entityFieldInfoList = args
				.getEntityFieldInfoList();

		String entityPackageName = args.getPackageNameInfo()
				.getEntityPackageName();
		String daoPackageName = args.getPackageNameInfo().getDaoPackageName();
		String servicePackageName = args.getPackageNameInfo()
				.getServicPackageName();
		String actionPackageName = args.getPackageNameInfo()
				.getActionPackageName();

		String entityClassName = args.getClassNameInfo().getEntityClassName();
		String daoClassName = args.getClassNameInfo().getDaoClassName();
		String serviceClassName = args.getClassNameInfo().getServicClassName();
		String actionClassName = args.getClassNameInfo().getActionClassName();

		String entityObjectName = args.getClassNameInfo().getEntityObjectName();
		String daoObjectName = args.getClassNameInfo().getDaoObjectName();
		String serviceObjectName = args.getClassNameInfo()
				.getServicObjectName();
		String actionObjectName = args.getClassNameInfo().getActionObjectName();

		stringBuffer.append(TEXT_1);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_2);
		stringBuffer.append(modeName);
		stringBuffer.append(TEXT_3);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_4);
		stringBuffer.append(modeName);
		stringBuffer.append(TEXT_5);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_6);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_7);
		stringBuffer.append(TEXT_8_ID);

		for (EntityFieldInfo entityFieldInfo : entityFieldInfoList) {
			List<String> fieldNames = entityFieldInfo.getFieldNames();
			for (String fieldName : fieldNames) {

				stringBuffer.append(TEXT_8);
				stringBuffer.append(fieldName);
				stringBuffer.append(TEXT_9);
				stringBuffer.append(fieldName);
				stringBuffer.append(TEXT_10);

			}
		}

		stringBuffer.append(TEXT_11);
		stringBuffer.append(TEXT_12_ID);
		for (EntityFieldInfo entityFieldInfo : entityFieldInfoList) {
			List<String> fieldNames = entityFieldInfo.getFieldNames();
			for (String fieldName : fieldNames) {

				stringBuffer.append(TEXT_12);
				stringBuffer.append(fieldName);
				stringBuffer.append(TEXT_13);

			}
		}

		stringBuffer.append(TEXT_14);
		stringBuffer.append(modeName);
		stringBuffer.append(TEXT_15);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_16);
		stringBuffer.append(modeName);
		stringBuffer.append(TEXT_17);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_18);
		stringBuffer.append(modeName);
		stringBuffer.append(TEXT_19);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_20);
		stringBuffer.append(modeName);
		stringBuffer.append(TEXT_21);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_22);
		stringBuffer.append(TEXT_23);
		return stringBuffer.toString();
	}
}
